package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.dao.Dao;
import team.se.tms.dto.Page;
import team.se.tms.dto.ParameterPage;
import team.se.tms.dto.TaskDetail;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.TaskInfo;
import team.se.tms.service.TaskInfoService;
import team.se.tms.util.KeyUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务信息业务层，具体实现任务信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/11/27
 */
@Slf4j
@Service("TaskInfoServiceImpl")
public class TaskInfoServiceImpl extends BaseServiceImpl implements TaskInfoService {
    @Resource(name = "DaoImpl")
    private Dao dao;

    /**
     * 根据授课id获取任务详细信息
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return 任务详细信息列表
     * @throws Exception 异常
     * @create on 2018/12/6 by winkyle
     */
    @Override
    public List<TaskDetail> listTaskDetails(@SpecialCheck("Pk") String teachCourseId, Page page) throws Exception {
        return dao.selectList("TaskInfoMapper.selectTaskDetailsByTeachCourseId",getParameterPage(page,"teachCourseId",teachCourseId));
    }

    /**
     * 根据授课id获取任务详细信息
     *
     * @param teachCourseId 授课id
     * @return 任务详细信息列表
     * @throws Exception 异常
     * @create on 2018/12/10 by qingx
     */
    @Override
    public List<TaskDetail> listTaskDetails(String teachCourseId) throws Exception {
        return dao.selectList("TaskInfoMapper.selectTaskDetailsByTeachCourseId",teachCourseId);
    }

    /**
     * 根据授课id获取任务信息
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return 任务信息列表
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    public List<TaskInfo> listTaskInfos(@SpecialCheck("Pk") String teachCourseId, Page page) throws Exception {
        return dao.selectList("TaskInfoMapper.selectTaskInfosByTeachCourseId",getParameterPage(page,"teachCourseId",teachCourseId));
    }

    /**
     * 保存任务信息
     *
     * @param taskInfo 任务信息
     * @return 保存结果
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    @CheckFormat({"taskId"})
    public Boolean saveTaskInfo(TaskInfo taskInfo) throws Exception {
        taskInfo.setTaskId(KeyUtil.generatePrimaryKey());
        int insertResult = dao.insert("TaskInfoMapper.insertTaskInfo",taskInfo);
        if(insertResult<=0){
            throw new InsertException();
        }
        return true;
    }

    /**
     * 修改任务信息
     *
     * @param taskInfo 任务信息
     * @return 修改结果
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */

    @Override
    @CheckFormat
    public Boolean modifyTaskInfo(TaskInfo taskInfo) throws Exception {
        int updateResult = dao.update("TaskInfoMapper.updateTaskInfoByTaskId",taskInfo);
        if(updateResult<=0){
            throw new UpdateException();
        }
        return true;
    }
}
